<template>
  <div class="m-img-group-upload">
    <div
      class="m-img-group-item"
      v-for="(item,index) in value"
      :key="index"
      :style="{
        width: width,
        height: height,
        backgroundImage: `url(${item})`
      }">
       <i class="m-img-group-remove iconfont" @click="handleRemove(index)">&#xe641;</i>
    </div>
    <img-upload v-bind="$props" :value="[]" :limit="myLimit" @change="handleChange" />
  </div>
</template>

<script>
import ImgUpload from './ImgUpload'
export default {
  components: {
    ImgUpload
  },
  props: {
    ...ImgUpload.props,
    value: {
      type: Array,
      default () {
        return []
      }
    },
    limit: {
      type: Number,
      default: 0
    }
  },
  computed: {
    myLimit () {
      if (this.limit === 0) {
        return this.limit
      } else {
        return this.limit - this.value.length
      }
    }
  },
  methods: {
    handleChange (value) {
      let myValue = [...this.value]
      myValue = myValue.concat(value)
      this.$emit('input', myValue).$emit('change', myValue)
    },
    handleRemove (index) {
      let myValue = [...this.value]
      myValue.splice(index, 1)
      this.$emit('input', myValue).$emit('change', myValue)
    }
  }
}
</script>
<style lang="scss">
  .m-img-group{
    &-item{
      box-sizing:border-box;
      border:1px solid $border-color-base;
      display:inline-block;
      background-size: cover;
      background-repeat: no-repeat;
      width:2rem;
      height:2rem;
      position:relative;
      vertical-align: middle;
      margin-right:5px;
      margin-bottom:5px;
    }
    &-remove{
      position:absolute;
      background:rgba(0,0,0,0.2);
      width:0.4rem;
      height:0.4rem;
      line-height: 0.4rem;
      text-align:center;
      right:0;
      top:0;
      color:#fff;
    }
  }
</style>
